﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Entities;
using DiemChapNhanThe.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class TinTucND : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string idTintuc = Request.Params["Id"];
            if (idTintuc != null)
            {
                int idTT = Int32.Parse(idTintuc);
                CapNhat_LuotXem(idTT);
                Bind_Data_NoidungTinTuc(idTT);
            }
            else
            {
                //
            }
        }
    }

    /// <summary>
    /// Hien thi noi dung cua tin tuc
    /// </summary>
    /// <param name="idTinTuc"></param>
    protected void Bind_Data_NoidungTinTuc(int idTinTuc)
    {
        int ToTalRows = 0;
        string WhereClause = "1=1";
        WhereClause += " and Id = " + idTinTuc;        

        TList<TinTuc> Tlist_TinTuc = DataRepository.TinTucProvider.GetPaged(WhereClause, "Id DESC", 0, 0, out ToTalRows);
                
        if (Tlist_TinTuc.Count > 0)
        {
            ltrTieude.Text = Tlist_TinTuc[0].TieuDe;
            dlNoidungTintuc.DataSource = Tlist_TinTuc;
            dlNoidungTintuc.DataBind();
        }
    }

    protected void CapNhat_LuotXem(int idTinTuc)
    {
        int ToTalRows = 0;
        string WhereClause = "1=1";
        WhereClause += " and Id = " + idTinTuc;
        int _view = 0;

        TList<TinTuc> Tlist_TinTuc = DataRepository.TinTucProvider.GetPaged(WhereClause, "Id DESC", 0, 0, out ToTalRows);
        _view += Tlist_TinTuc[0].LuotXem;
        _view += 1;

        string query = string.Empty;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        conn.Open();
        query = "UPDATE TinTuc SET LuotXem = '" + _view + "' where Id = '" + idTinTuc + "'";
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = conn;
        sqlCommand.CommandText = query;
        sqlCommand.ExecuteNonQuery();
        conn.Close();
    }
}